package jcu.ict.profaid;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class FormResultPercentage extends Activity {
	private AnswerDbAdapter adb;
	private QuestionDbAdapter qdb;
	private Cursor aCursor;
	private String questionID;
	private String courseID;

	private TextView txtAnswerA;
	private TextView txtAnswerB;
	private TextView txtAnswerC;
	private TextView txtAnswerD;
	private TextView txtAnswerE;

	private TextView txtPercentageA;
	private TextView txtPercentageB;
	private TextView txtPercentageC;
	private TextView txtPercentageD;
	private TextView txtPercentageE;
	private String user_id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.form_percentage);

		qdb = new QuestionDbAdapter(getBaseContext());
		adb = new AnswerDbAdapter(getBaseContext());
		new CalculatePercentage();
		adb.open();
		qdb.open();

		Bundle bundle = this.getIntent().getExtras();
		questionID = bundle.getString("setQuestionId");
		courseID = bundle.getString("setCourseId");
		user_id = (String) bundle.getCharSequence("setUserId");
		
		Button calculate = (Button) findViewById(R.id.btnCalculate);

		txtAnswerA = (TextView) findViewById(R.id.AnswerA);
		txtAnswerB = (TextView) findViewById(R.id.AnswerB);
		txtAnswerC = (TextView) findViewById(R.id.AnswerC);
		txtAnswerD = (TextView) findViewById(R.id.AnswerD);
		txtAnswerE = (TextView) findViewById(R.id.AnswerE);

		txtPercentageA = (TextView) findViewById(R.id.PercentageA);
		txtPercentageB = (TextView) findViewById(R.id.PercentageB);
		txtPercentageC = (TextView) findViewById(R.id.PercentageC);
		txtPercentageD = (TextView) findViewById(R.id.PercentageD);
		txtPercentageE = (TextView) findViewById(R.id.PercentageE);

		txtPercentageA.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
		txtPercentageB.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
		txtPercentageC.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
		txtPercentageD.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
		txtPercentageE.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);

		initViews();

		TextView txtQuestion = (TextView) findViewById(R.id.QuestionDesc);

		Cursor xCursor = qdb.populateQuestionDesc(questionID);
		xCursor.moveToFirst();

		try {
			String question = xCursor.getString(0);
			txtQuestion.setText(question);
		} catch (Exception e) {
		}

		aCursor = adb.PopulateAnswer(user_id,questionID);
		aCursor.moveToFirst();

		displayToScreen(aCursor.getCount());

		calculate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				adb.open();
				qdb.open();
				initViews();

				TextView txtQuestion = (TextView) findViewById(R.id.QuestionDesc);

				Cursor xCursor = qdb.populateQuestionDesc(questionID);
				xCursor.moveToFirst();

				String question = xCursor.getString(0);
				txtQuestion.setText(question);

				aCursor = adb.PopulateAnswer(user_id,questionID);
				aCursor.moveToFirst();

				displayToScreen(aCursor.getCount());
			}
		});

		adb.close();
		qdb.close();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	public void initViews() {
		Cursor iTimeDate = adb.getTimeCompare();
		iTimeDate.moveToFirst();

		long dTime = iTimeDate.getLong(0);

		Uri uriSms = Uri.parse("content://sms/inbox");
		Cursor cursor = getContentResolver().query(uriSms,
				new String[] { "_id", "address", "date", "body" },
				"date" + ">" + dTime, null, null);

		String body;
		String smsId;
		String number;

		if (cursor.moveToFirst()) {
			adb.updateTimeFlag(cursor.getLong(2));
			do {
				body = cursor.getString(3);
				smsId = cursor.getString(0);
				number = cursor.getString(1);
				if (checkSMSvalue(body) == true) {
					insertPhoneBook(courseID, questionID, number);
					deleteSms(smsId);
				}
			} while (cursor.moveToNext());
		} else {
		}
	}

	public boolean checkSMSvalue(String answer) {
		boolean check = false;
		try {
			int iAnswer = Integer.parseInt(answer);

			Cursor cAnswer = adb.getAnswer(iAnswer);
			cAnswer.moveToFirst();

			for (int i = 0; i < cAnswer.getCount(); i++)
				for (int j = 0; j < cAnswer.getColumnCount(); j++) {
					Cursor cAnswerSum = adb.getSum(cAnswer.getInt(j));
					cAnswerSum.moveToFirst();

					int x = cAnswerSum.getInt(0);

					if (adb.updateAnswerSum(cAnswer.getInt(j), x)) {
						check = true;
						Toast.makeText(this, "Update successful.",
								Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(this, "Update failed.",
								Toast.LENGTH_LONG).show();
					}
				}
		} catch (Exception e) {
		}
		return check;
	}

	public boolean deleteSms(String smsId) {
		boolean isSmsDeleted = false;
		try {
			this.getContentResolver().delete(
					Uri.parse("content://sms/" + smsId), null, null);
			isSmsDeleted = true;

		} catch (Exception ex) {
			isSmsDeleted = false;
		}
		return isSmsDeleted;
	}

	public boolean insertPhoneBook(String courseId, String questionId,
			String number) {
		boolean isSmsDeleted = false;
		try {
			PhoneDBAdapter pdb = new PhoneDBAdapter(getBaseContext());
			pdb.open();
			Cursor pCount = pdb.searchNumber(number);
			if (pCount.getCount() <= 0) {
				pdb.insertAnswerList(user_id,number, questionId, courseId);
			}
			pdb.close();

		} catch (Exception ex) {
			isSmsDeleted = false;
		}
		return isSmsDeleted;
	}

	public void displayToScreen(int count) {
		if (count == 2) {
			String answerA = aCursor.getString(0);
			String sumA = aCursor.getString(1);
			String IdA = aCursor.getString(2);

			aCursor.moveToNext();
			String answerB = aCursor.getString(0);
			String sumB = aCursor.getString(1);
			String IdB = aCursor.getString(2);

			int aSum = Integer.parseInt(sumA);
			int bSum = Integer.parseInt(sumB);

			int sumAll = aSum + bSum;

			double percentageA = CalculatePercentage.percentage(aSum, sumAll);
			double percentageB = CalculatePercentage.percentage(bSum, sumAll);

			if (Double.isNaN(percentageA)) {
				percentageA = 0.0;
			}
			if (Double.isNaN(percentageB)) {
				percentageB = 0.0;
			}

			txtPercentageA
					.setText(String.format("%2.01f", percentageA) + "%  ");
			txtPercentageB
					.setText(String.format("%2.01f", percentageB) + "%  ");

			txtAnswerA.setText("A. " + answerA + "- " + "Answer Id = " + IdA);
			txtAnswerB.setText("B. " + answerB + "- " + "Answer Id = " + IdB);

		} else if (count == 3) {
			String answerA = aCursor.getString(0);
			String sumA = aCursor.getString(1);
			String IdA = aCursor.getString(2);

			aCursor.moveToNext();
			String answerB = aCursor.getString(0);
			String sumB = aCursor.getString(1);
			String IdB = aCursor.getString(2);

			aCursor.moveToNext();
			String answerC = aCursor.getString(0);
			String sumC = aCursor.getString(1);
			String IdC = aCursor.getString(2);

			int aSum = Integer.parseInt(sumA);
			int bSum = Integer.parseInt(sumB);
			int cSum = Integer.parseInt(sumC);

			int sumAll = aSum + bSum + cSum;

			double percentageA = CalculatePercentage.percentage(aSum, sumAll);
			double percentageB = CalculatePercentage.percentage(bSum, sumAll);
			double percentageC = CalculatePercentage.percentage(cSum, sumAll);

			if (Double.isNaN(percentageA)) {
				percentageA = 0.0;
			}
			if (Double.isNaN(percentageB)) {
				percentageB = 0.0;
			}
			if (Double.isNaN(percentageC)) {
				percentageC = 0.0;
			}

			txtPercentageA.setText(String.format("%2.01f", percentageA) + "% ");
			txtPercentageB.setText(String.format("%2.01f", percentageB) + "% ");
			txtPercentageC.setText(String.format("%2.01f", percentageC) + "% ");

			txtAnswerA.setText("A. " + answerA + "- " + "Answer Id = " + IdA);
			txtAnswerB.setText("B. " + answerB + "- " + "Answer Id = " + IdB);
			txtAnswerC.setText("C. " + answerC + "- " + "Answer Id = " + IdC);

		} else if (count == 4) {
			String answerA = aCursor.getString(0);
			String sumA = aCursor.getString(1);
			String IdA = aCursor.getString(2);

			aCursor.moveToNext();
			String answerB = aCursor.getString(0);
			String sumB = aCursor.getString(1);
			String IdB = aCursor.getString(2);

			aCursor.moveToNext();
			String answerC = aCursor.getString(0);
			String sumC = aCursor.getString(1);
			String IdC = aCursor.getString(2);

			aCursor.moveToNext();
			String answerD = aCursor.getString(0);
			String sumD = aCursor.getString(1);
			String IdD = aCursor.getString(2);

			int aSum = Integer.parseInt(sumA);
			int bSum = Integer.parseInt(sumB);
			int cSum = Integer.parseInt(sumC);
			int dSum = Integer.parseInt(sumD);

			int sumAll = aSum + bSum + cSum + dSum;

			double percentageA = CalculatePercentage.percentage(aSum, sumAll);
			double percentageB = CalculatePercentage.percentage(bSum, sumAll);
			double percentageC = CalculatePercentage.percentage(cSum, sumAll);
			double percentageD = CalculatePercentage.percentage(dSum, sumAll);

			if (Double.isNaN(percentageA)) {
				percentageA = 0.0;
			}
			if (Double.isNaN(percentageB)) {
				percentageB = 0.0;
			}
			if (Double.isNaN(percentageC)) {
				percentageC = 0.0;
			}
			if (Double.isNaN(percentageD)) {
				percentageD = 0.0;
			}

			txtPercentageA.setText(String.format("%2.01f", percentageA) + "% ");
			txtPercentageB.setText(String.format("%2.01f", percentageB) + "% ");
			txtPercentageC.setText(String.format("%2.01f", percentageC) + "% ");
			txtPercentageD.setText(String.format("%2.01f", percentageD) + "% ");

			txtAnswerA.setText("A. " + answerA + "- " + "Answer Id = " + IdA);
			txtAnswerB.setText("B. " + answerB + "- " + "Answer Id = " + IdB);
			txtAnswerC.setText("C. " + answerC + "- " + "Answer Id = " + IdC);
			txtAnswerD.setText("D. " + answerD + "- " + "Answer Id = " + IdD);

		}

		else if (count == 5) {
			String answerA = aCursor.getString(0);
			String sumA = aCursor.getString(1);
			String IdA = aCursor.getString(2);

			aCursor.moveToNext();
			String answerB = aCursor.getString(0);
			String sumB = aCursor.getString(1);
			String IdB = aCursor.getString(2);

			aCursor.moveToNext();
			String answerC = aCursor.getString(0);
			String sumC = aCursor.getString(1);
			String IdC = aCursor.getString(2);

			aCursor.moveToNext();
			String answerD = aCursor.getString(0);
			String sumD = aCursor.getString(1);
			String IdD = aCursor.getString(2);

			aCursor.moveToNext();
			String answerE = aCursor.getString(0);
			String sumE = aCursor.getString(1);
			String IdE = aCursor.getString(2);

			int aSum = Integer.parseInt(sumA);
			int bSum = Integer.parseInt(sumB);
			int cSum = Integer.parseInt(sumC);
			int dSum = Integer.parseInt(sumD);
			int eSum = Integer.parseInt(sumE);

			int sumAll = aSum + bSum + cSum + dSum + eSum;

			double percentageA = CalculatePercentage.percentage(aSum, sumAll);
			double percentageB = CalculatePercentage.percentage(bSum, sumAll);
			double percentageC = CalculatePercentage.percentage(cSum, sumAll);
			double percentageD = CalculatePercentage.percentage(dSum, sumAll);
			double percentageE = CalculatePercentage.percentage(eSum, sumAll);

			if (Double.isNaN(percentageA)) {
				percentageA = 0.0;
			}
			if (Double.isNaN(percentageB)) {
				percentageB = 0.0;
			}
			if (Double.isNaN(percentageC)) {
				percentageC = 0.0;
			}
			if (Double.isNaN(percentageD)) {
				percentageD = 0.0;
			}
			if (Double.isNaN(percentageE)) {
				percentageE = 0.0;
			}

			txtPercentageA.setText(String.format("%2.01f", percentageA) + "% ");
			txtPercentageB.setText(String.format("%2.01f", percentageB) + "% ");
			txtPercentageC.setText(String.format("%2.01f", percentageC) + "% ");
			txtPercentageD.setText(String.format("%2.01f", percentageD) + "% ");
			txtPercentageE.setText(String.format("%2.01f", percentageE) + "% ");

			txtAnswerA.setText("A. " + answerA + "- " + "Answer Id = " + IdA);
			txtAnswerB.setText("B. " + answerB + "- " + "Answer Id = " + IdB);
			txtAnswerC.setText("C. " + answerC + "- " + "Answer Id = " + IdC);
			txtAnswerD.setText("D. " + answerD + "- " + "Answer Id = " + IdD);
			txtAnswerE.setText("E. " + answerE + "- " + "Answer Id = " + IdE);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainQuestion.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundle = new Bundle();
			bundle.putString("setInfoCourse", courseID);
			bundle.putCharSequence("setUserId", user_id);
			intent.putExtras(bundle);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Intent in = new Intent(FormResultPercentage.this,
					MainQuestion.class);
			Bundle bundle = new Bundle();
			bundle.putString("setInfoCourse", courseID);
			bundle.putCharSequence("setUserId", user_id);
			in.putExtras(bundle);
			startActivity(in);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
